﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Dao;
using Chinaer.Model.Common;
using Chinaer.Core.Helper;

namespace Chinaer.Bll
{
    public class MenuCategory : Chinaer.IBll.IMenuCategory
    {
        Chinaer.IDao.IMenuCategoryDAO menuCategoryDAO = new Chinaer.Dao.MenuCategoryDAO();
        public JsonResult Add(DTO.MenuCategoryDTO t)
        {
            JsonResult jsonResult = new JsonResult();
            if (t == null)
            {
                jsonResult.Success = false;
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.EntityIsNull;
                jsonResult.ErrorMessage = "MenucategoryDTO为空";
                return jsonResult;
            }
            if (menuCategoryDAO.GetByName(t.Name) != null)
            {
                //表示文件已经存在
                jsonResult.LoginStatus = Chinaer.Core.BaseCore.LoginStatus.IsExists;
                jsonResult.ErrorMessage = "添加的MenucategoryName已经存在";
                return jsonResult;
            }
            Chinaer.Model.Entities.Model_MenuCategory model_MenuCategory = new Model.Entities.Model_MenuCategory();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_MenuCategory);
            if (string.IsNullOrEmpty(model_MenuCategory.ID) || model_MenuCategory.ID.Trim() == "")
            {
                model_MenuCategory.ID = Chinaer.Core.Helper.GenerateIDHelper.GenerateID();
            }
            jsonResult.Success = menuCategoryDAO.Add(model_MenuCategory);
            return jsonResult;
        }

        public JsonResult Delete(DTO.MenuCategoryDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Chinaer.Model.Entities.Model_MenuCategory model_MenuCategory = new Model.Entities.Model_MenuCategory();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_MenuCategory);
            jsonResult.Success = menuCategoryDAO.Delete(model_MenuCategory);
            return jsonResult;
        }

        public JsonResult Delete(string id)
        {
            JsonResult jsonResult = new JsonResult();
            jsonResult.Success = menuCategoryDAO.Delete(id);
            return jsonResult;
        }

        public JsonResult Update(DTO.MenuCategoryDTO t)
        {
            if (t.CheckIsNull().Success == false)
            {
                return t.CheckIsNull();
            }
            JsonResult jsonResult = new JsonResult();
            Chinaer.Model.Entities.Model_MenuCategory model_MenuCategory = new Model.Entities.Model_MenuCategory();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(t, model_MenuCategory);
            jsonResult.Success = menuCategoryDAO.Update(model_MenuCategory);
            return jsonResult;
        }

        public List<DTO.MenuCategoryDTO> GetAllMenuCategory()
        {
            if (Chinaer.Cache.CacheHelper.Memcached_KeyExists("MenuCategoryAll"))
            {
                return (List<DTO.MenuCategoryDTO>)Chinaer.Cache.CacheHelper.Memcached_Get("MenuCategoryAll");
            }
            List<Model.Entities.Model_MenuCategory> list_Menucategory = (List<Model.Entities.Model_MenuCategory>)menuCategoryDAO.GetAll();
            List<DTO.MenuCategoryDTO> listMenuCategoryDTO = new List<DTO.MenuCategoryDTO>();

            foreach (var item in list_Menucategory)
            {
                DTO.MenuCategoryDTO menuCategoryDTO = new DTO.MenuCategoryDTO();
                Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(item, menuCategoryDTO);
                listMenuCategoryDTO.Add(menuCategoryDTO);
            }
            Chinaer.Cache.CacheHelper.Memcached_Set("MenuCategoryAll", listMenuCategoryDTO);
            return listMenuCategoryDTO;
        }


        public DTO.MenuCategoryDTO GetByID(string id)
        {
            Model.Entities.Model_MenuCategory model_MenuCategory = new Model.Entities.Model_MenuCategory();
            DTO.MenuCategoryDTO menuCategoryDTO = new DTO.MenuCategoryDTO();
            model_MenuCategory = menuCategoryDAO.GetByID(id);
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_MenuCategory, menuCategoryDTO);
            return menuCategoryDTO;
        }


        public DTO.MenuCategoryDTO GetMenucategoryByName(string name)
        {
            Model.Entities.Model_MenuCategory model_Menucategory = new Model.Entities.Model_MenuCategory();
            model_Menucategory = menuCategoryDAO.GetByName(name);
            DTO.MenuCategoryDTO menucategoryDTO = new DTO.MenuCategoryDTO();
            Chinaer.Core.Helper.DTOtoEntityObjectHelper.Instance.DTOtoEntityObject(model_Menucategory, menucategoryDTO);
            return menucategoryDTO;
        }
    }
}
